﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class RoleBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public RoleBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string Name)
        {
            if (DAO.T_SYS_ROLEs.Where(t => t.NAME == Name).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_SYS_ROLE> GetRoleList()
        {
            var list = DAO.T_SYS_ROLEs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }
            else {
                list = list.OrderBy(t=>t.NAME);
            }

            return list;
        }

        public T_SYS_ROLE GetRoleByID(string ID)
        {
            return DAO.T_SYS_ROLEs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddRole(T_SYS_ROLE Role)
        {
            try
            {
                Role.ID = DAO.GetGUID();

                DAO.T_SYS_ROLEs.InsertOnSubmit(Role);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddRole(T_SYS_ROLE Role, out string RoleGuid)
        {
            try
            {
                Role.ID = DAO.GetGUID();
                
                DAO.T_SYS_ROLEs.InsertOnSubmit(Role);
                DAO.SubmitChanges();

                RoleGuid = Role.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                RoleGuid = "";
                return -1;
            }
        }

        public int UpdateRole(T_SYS_ROLE Role)
        {
            try
            {
                T_SYS_ROLE updateEntity = DAO.T_SYS_ROLEs.SingleOrDefault(t=>t.ID.ToString()==Role.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.NAME = Role.NAME;
                updateEntity.Description = Role.Description;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateRole(T_SYS_ROLE Role, List<string> AuthorityIDs)
        {
            try
            {
                T_SYS_ROLE updateEntity = DAO.T_SYS_ROLEs.SingleOrDefault(t => t.ID.ToString() == Role.ID.ToString());
                if (updateEntity == null)
                    return -1;

                updateEntity.NAME = Role.NAME;
                updateEntity.Description = Role.Description;

                List<T_RoleAuthority> addList = new List<T_RoleAuthority>();

                foreach (string id in AuthorityIDs)
                {
                    if (updateEntity.T_RoleAuthorities.Where(t => t.AuthorityID.ToString() == id).Count() == 0)
                    {
                        T_RoleAuthority roleAuthority = new T_RoleAuthority();

                        roleAuthority.ID = DAO.GetGUID();
                        roleAuthority.AuthorityID = new Guid(id);

                        addList.Add(roleAuthority);
                    }
                }

                List<T_RoleAuthority> deleteList = new List<T_RoleAuthority>();

                foreach (T_RoleAuthority item in updateEntity.T_RoleAuthorities)
                {
                    if (AuthorityIDs.Where(t => t == item.AuthorityID.ToString()).Count() == 0)
                    {
                        deleteList.Add(item);
                    }
                }

                foreach (T_RoleAuthority deleteItem in deleteList)
                {
                    updateEntity.T_RoleAuthorities.Remove(deleteItem);
                }

                updateEntity.T_RoleAuthorities.AddRange(addList);

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteRole(string ID)
        {
            try
            {
                T_SYS_ROLE deleteEntity = DAO.T_SYS_ROLEs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_SYS_ROLEs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
